package com.powernode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.powernode.domain.SysRole;
import com.powernode.domain.SysRoleMenu;
import com.powernode.domain.SysUser;
import com.powernode.service.SysRoleMenuService;
import com.powernode.service.SysRoleService;
import com.powernode.mapper.SysRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole>
        implements SysRoleService {
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @Override
    public Page<SysRole> getSysUserPage(Page<SysRole> page, SysRole sysRole) {
        Page<SysRole> pageRole = sysRoleMapper.selectPage(page, new LambdaQueryWrapper<SysRole>()
                //进行模糊查询 如果 没传入值则全部查询
                .like(StringUtils.hasText(sysRole.getRoleName()), SysRole::getRoleName, sysRole.getRoleName()));
        return pageRole;
    }

    @Override
    @Transactional
    public void addRoleAndAuth(SysRole sysRole) {
        sysRole.setCreateTime(new Date());
        sysRoleMapper.insert(sysRole);
        //获取到权限id
        List<Long> menuIdList = sysRole.getMenuIdList();
        List<SysRoleMenu> sysRoleMenus = new ArrayList<>();
        menuIdList.forEach(menuId -> {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setMenuId(menuId);
            sysRoleMenu.setRoleId(sysRole.getRoleId());
            sysRoleMenus.add(sysRoleMenu);
        });
        sysRoleMenuService.saveBatch(sysRoleMenus);
    }
}




